package com.quantumoctopus.magiceatball;



import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class OptionsActivity extends Activity {

	
	public static final String PREFS_EATBALL = "EatBallPrefsFile";
	static SharedPreferences settings;
	SharedPreferences.Editor editor;
	private RadioGroup radioGroup1;
	private RadioGroup radioGroup2;
	private RadioButton radio0;
	private RadioButton radio1;
	private RadioButton radio2;
	private RadioButton radio3;
	private RadioButton radioButton0;
	private RadioButton radioButton1;
	private RadioButton radioButton2;
	private RadioButton radioButton3;
	
	private TextView pricerangetext;

	private CheckBox uniquecb;
	private CheckBox opencb;
	private Button clearLogButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.options_activity_layout);
		clearLogButton = (Button) findViewById(R.id.clearLogButton);
		radioGroup1= (RadioGroup)findViewById(R.id.radioGroup1);
		radioGroup2= (RadioGroup)findViewById(R.id.radioGroup2);
		radio0=(RadioButton)findViewById(R.id.radio0);
		radio1=(RadioButton)findViewById(R.id.radio1);
		radio2=(RadioButton)findViewById(R.id.radio2);
		radio3=(RadioButton)findViewById(R.id.radio3);
		radioButton0=(RadioButton)findViewById(R.id.radioButton0);
		radioButton1=(RadioButton)findViewById(R.id.radioButton1);
		radioButton2=(RadioButton)findViewById(R.id.radioButton2);
		radioButton3=(RadioButton)findViewById(R.id.radioButton3);
		uniquecb=(CheckBox)findViewById(R.id.uniquecb);
		//opencb=(CheckBox)findViewById(R.id.opencb);
		pricerangetext = (TextView) findViewById(R.id.pricerangetext);
		settings = getSharedPreferences(PREFS_EATBALL, 0);
		radio0.setOnClickListener(listenerprice);
		radio1.setOnClickListener(listenerprice);
		radio2.setOnClickListener(listenerprice);
		radio3.setOnClickListener(listenerprice);
		//uniquecb.setOnClickListener(listeneruniqcb);
		radioButton0.setOnClickListener(listenerdistance);
		radioButton1.setOnClickListener(listenerdistance);
		radioButton2.setOnClickListener(listenerdistance);
		radioButton3.setOnClickListener(listenerdistance);
		//radioGroup1.clearCheck();
		//radioGroup2.clearCheck();
		//addListenerCheckuni();
		setListenercbu();
		//setListenercbo();
		if(settings.getBoolean("newloc", false)==true){uniquecb.setChecked(true);}else{uniquecb.setChecked(false);}
		//if(settings.getBoolean("open", false)==true){opencb.setChecked(true);}else{opencb.setChecked(false);}
		if(settings.getInt("price", 4)==4){radio3.setChecked(true);}
		if(settings.getInt("price", 4)==3){radio2.setChecked(true);}
		if(settings.getInt("price", 4)==2){radio1.setChecked(true);}
		if(settings.getInt("price", 4)==1){radio0.setChecked(true);}
		
		if(settings.getString("distance", "2000").equals("3200")==true){radioButton0.setChecked(true);}
		if(settings.getString("distance", "2000").equals("8000")==true){radioButton1.setChecked(true);}
		if(settings.getString("distance", "2000").equals("16000")==true){radioButton2.setChecked(true);}
		if(settings.getString("distance", "2000").equals("32000")==true){radioButton3.setChecked(true);}
		
		

	
	}
	
	private void setListenercbu() {
		uniquecb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				editor = settings.edit();

				if (isChecked) {
					uniquecb.setChecked(true);
					editor.putBoolean("newloc", true);
					editor.commit();
					Toast.makeText(OptionsActivity.this, "New Locations only",
							Toast.LENGTH_LONG).show();
				} else {
					uniquecb.setChecked(false);
					editor.putBoolean("newloc", false);
					editor.commit();
					Toast.makeText(OptionsActivity.this,
							"Old locations are accepted", Toast.LENGTH_LONG)
							.show();
				}

			}
		});
	}
	
	
	private void setListenercbo() {
		opencb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				editor = settings.edit();

				if (isChecked) {
					editor.putBoolean("open", true);
					editor.commit();
					Toast.makeText(OptionsActivity.this, "Locations without posted hours will not be included",
							Toast.LENGTH_LONG).show();
				} else {
					editor.putBoolean("open", false);
					editor.commit();
					Toast.makeText(OptionsActivity.this,
							"Old locations are accepted", Toast.LENGTH_LONG)
							.show();
				}

			}
		});
	}
	
	
//	private void addListenerCheckuni() {
//		// TODO Auto-generated method stub
//		uniquecb.setOnClickListener(new OnClickListener() {
//
//	        @Override
//	        public void onClick(View v) {
//	            // TODO Auto-generated method stub
//	            if(uniquecb.isChecked()){
//	            	editor.putBoolean("newloc", true);
//					Toast.makeText(OptionsActivity.this, "New Locations only",
//							Toast.LENGTH_LONG).show();
//	            }else{
//	            	editor.putBoolean("newloc", false);
//					Toast.makeText(OptionsActivity.this,
//							"Old locations are accepted", Toast.LENGTH_LONG).show();
//	            }
//	        }
//	    });
//	}
	
	
	

	
	
	
//	OnClickListener listeneruniqcb = new OnClickListener() {
//		@Override
//		public void onClick(View v) {
//			if (((CheckBox) v).isChecked()) {
//				editor.putBoolean("newloc", true);
//				Toast.makeText(OptionsActivity.this, "New Locations only",
//						Toast.LENGTH_LONG).show();
//			}
//			if (((CheckBox) v).isChecked() != true) {
//				editor.putBoolean("newloc", false);
//				Toast.makeText(OptionsActivity.this,
//						"Old locations are accepted", Toast.LENGTH_LONG).show();
//			}
//		}
//	};
	
	OnClickListener listenerprice = new OnClickListener() {
        @Override
        public void onClick(View v) {
        	editor = settings.edit();
               RadioButton rb = (RadioButton) v;
			if (rb.getId() == R.id.radio0) {
				editor.putBoolean("pricecare", true);
				editor.putInt("price", 1);
			}
			if (rb.getId() == R.id.radio1) {
				editor.putBoolean("pricecare", true);
				editor.putInt("price", 2);
			}
			if (rb.getId() == R.id.radio2) {
				editor.putBoolean("pricecare", true);
				editor.putInt("price", 3);
			}
			if (rb.getId() == R.id.radio3) {
				editor.putBoolean("pricecare", false);
				editor.putInt("price", 4);
				Toast.makeText(OptionsActivity.this, "Checking no limit will improve search speed",
						Toast.LENGTH_SHORT).show();
			}
               
			editor.commit();
               
               

//               Toast.makeText(OptionsActivity.this, rb.getText(), 
//               Toast.LENGTH_SHORT).show();
        }
  };
  
  
	OnClickListener listenerdistance = new OnClickListener() {
		@Override
		public void onClick(View v) {
			editor = settings.edit();
			RadioButton rb = (RadioButton) v;
			if (rb.getId() == R.id.radioButton0) {
				editor.putString("distance", "3200");

			}
			if (rb.getId() == R.id.radioButton1) {
				editor.putString("distance", "8000");

			}
			if (rb.getId() == R.id.radioButton2) {
				editor.putString("distance", "16000");

			}
			if (rb.getId() == R.id.radioButton3) {
				editor.putString("distance", "32000");
			}
             
             
			editor.commit();

//             Toast.makeText(OptionsActivity.this, rb.getText(), 
//             Toast.LENGTH_SHORT).show();
      }
};

@Override
protected void onResume() {
	super.onResume();
}

@Override
protected void onPause() {
	super.onPause();
}

public void clearLog(View v) {

	editor = settings.edit();
	editor.putString("savedlogslist", "");
	editor.commit();
	
	
}

 

	
	
//	public void getRadioClick(View view) {
//		int selectedId = radioGroup1.getCheckedRadioButtonId();
//		radio0 = (RadioButton) findViewById(selectedId);
//		 
//		Toast.makeText(OptionsActivity.this,
//			radio0.getText(), Toast.LENGTH_SHORT).show();
//	}

}
